package com.example.last.controller;

import com.example.last.common.Result;
import com.example.last.entity.Notice;
import com.example.last.entity.Params;
import com.example.last.entity.Type;
import com.example.last.service.NoticeService;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RequestMapping("/notice")
@RestController
public class NoticeController {
    @Resource
   private NoticeService noticeService;
    @GetMapping("/findAll")
    public Result findAll(){
        List<Notice> list = noticeService.findAll();
        return Result.success(list);
    }
    
    @GetMapping("/search")
    public Result findBySearch(@Param("params") Params params){
        PageInfo<Notice> info = noticeService.findBySearch(params);
        return Result.success(info);
    }
    //新增
    @PostMapping
    public Result save(@RequestBody Notice notice){
        if(notice.getId() == null){
            noticeService.add(notice);
        }else{
            noticeService.update(notice);
        }
        return Result.success();
    }
    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        noticeService.delete(id);
        return Result.success();
    }

    //批量删除
    @PutMapping("/batch")
    public Result batchdel(@RequestBody List<Notice> list){
        for(Notice notice : list){
            noticeService.delete(notice.getId());
        }
        return Result.success();
    }

}
